<!DOCTYPE html>
<html>
<head>
    {include file="layout:_meta" /}
</head>
<body>
<div class="layui-form"  style="padding: 20px 30px 0 0;">
    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-block">
            <div id="xm-select"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="admin_name"  placeholder="请输入姓名（可不填）"  value="{:isset($list.admin_name)?$list.admin_name:''}" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">登录名</label>
        <div class="layui-input-block">
            <input type="text" name="username" value="{:isset($list.username)?$list.username:''}" placeholder="请输入用户名"  lay-reqtext="请输入管理员登录名" autocomplete="off" class="layui-input"
                   {if condition="!isset($list['id'])" } lay-verify="required|username"{/if}>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            {if condition="!isset($list['id'])"}
            <input type="password" name="password" lay-verify="required|password" placeholder="请输入密码" lay-reqtext="请输入登录密码"  autocomplete="off" class="layui-input">
            {else /}
            <input type="password" name="password" placeholder="重置密码，不填不修改" autocomplete="off" class="layui-input">
            {/if}
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input type="text" name="email" lay-verify="emptyEmail" value="{:isset($list.email)?$list.email:''}" placeholder="请输入邮箱（可不填）" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">审核状态</label>
        <div class="layui-input-block">
            {if condition="isset($list.state)&&$list.state==1"}
            <input type="checkbox" lay-filter="state"  checked value="1" name="state" lay-skin="switch" lay-text="通过|待审核">
            {else /}
            <input type="checkbox" lay-filter="state"  value="0" name="state" lay-skin="switch" lay-text="通过|待审核">
            {/if}
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="hidden" name="id" value="{:isset($list['id'])?$list['id']:''}">
        <input type="hidden" name="token" value="{:isset($list['token'])?$list['token']:''}">
        <input type="button" lay-submit lay-filter="addEdit" id="addEdit" value="确认">
    </div>
</div>
{include file="layout:_footer" /}
<script>
    layui.use(['form','xmSelect'], function(){
        var form = layui.form,$ = layui.$,xmSelect = layui.xmSelect;

        //角色多选框
        var xm_select = xmSelect.render({
            el: '#xm-select',
            tips: '请选择角色',
            prop: {name: 'title', value: 'id'},
            name: 'group_id',
            layVerify: 'required',
            data:[]
        });

        //获取数据以及赋值
        $.get("{:url('admin/edit')}",{id:"{:isset($list['id'])?$list['id']:''}"},function (res) {
            xm_select.update({data: res.roleList});
            xm_select.setValue(res.role)
        });

        form.verify({
            username: function (value, item) { //value：表单的值、item：表单的DOM对象
                var msg;
                $.ajax({
                    async: false,
                    url: "{:url('admin/check')}",
                    data: {username: value},
                    success: function (data) {
                        if (data.code) {
                            msg = data.msg;
                        }
                    }
                });
                return msg;
            },

            emptyEmail: function (value, item) { //value：表单的值、item：表单的DOM对象
                if(value){
                    if(!(/^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/.test(value))){
                        return '请输入合法的邮箱';
                    }
                }
            },
            password:function(value, item){
                if(value){
                    if(!(/^[\w\W]{6,16}$/.test(value))){
                        return '密码是6-16位的字母加数字';
                    }
                }
            }

        });

        form.on('switch(state)', function(data){
            this.value = this.checked?1:0;
        });
    })
</script>
</body>
</html>
